package com.jacen.aws.util;

import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 版权声明：Copyright(c) 2018 All Rights Reserved.
 *
 * @Author Jacen (295117485@qq.com)
 * @Date 2018/10/13 8:39
 * @Version 1.0
 * @Description String 工具类
 */
public class StringUtil {

    /**
     * 判断给定字符串是否为空
     *
     * @param s
     * @return
     */
    public static boolean isEmpty(String s) {
        return s == null || "".equals(s);
    }

    /**
     * 字符串分割 根据delimiter进行数据截取，ignoreEmpty=true 过滤结果集中的前后空字符串
     *
     * @param input       输入字符串
     * @param delimiter   分隔字符
     * @param ignoreEmpty 是否忽略空字符串
     * @return String[]
     */
    public static String[] split(String input, char delimiter, boolean ignoreEmpty) {
        if (input == null) {
            return new String[0];
        }
        input = input.trim();
        Vector<String> v = new Vector<String>();
        boolean moreTokens = true;
        String subString;
        while (moreTokens) {
            int tokenLocation = input.indexOf(delimiter);
            if (tokenLocation == 0) {
                subString = input.substring(0, tokenLocation);
                if (!ignoreEmpty) {
                    v.addElement(subString);
                }
                input = input.substring(tokenLocation + 1);
            } else if (tokenLocation > 0) {
                subString = input.substring(0, tokenLocation);
                v.addElement(subString);
                input = input.substring(tokenLocation + 1);
            } else {
                moreTokens = false;
                if (!ignoreEmpty || input.length() > 0) {
                    v.addElement(input);
                }
            }
        }
        String[] res = new String[v.size()];
        for (int i = 0; i != res.length; i++) {
            res[i] = (String) v.elementAt(i);
        }
        return res;
    }

    public static int getCharIndexTimes(String str, String a, int num) {
        Pattern pattern = Pattern.compile("[" + a + "]");
        Matcher findMatcher = pattern.matcher(str);
        int number = 0;
        while (findMatcher.find()) {
            number++;
            if (number == num) {
                break;
            }
        }
        return findMatcher.start();
    }


}
